/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2023-08-25     wyj       the first version
 */
#ifndef _FLOW_SENSOR_HPP_
#define _FLOW_SENSOR_HPP_

#include <vector>
#include <math.h>
#include "drivers_module.hpp"

typedef enum
{
    pmw3901_status_fail,
    pmw3901_status_ok,
} pmw3901_status;

typedef enum
{
    PMW3901_REG_PRODUCT_ID                  =0x00,        /**< product id register */
    PMW3901_REG_REVISION_ID                 =0x01,        /**< revision id register */
    PMW3901_REG_MOTION                      =0x02,        /**< motion register */
    PMW3901_REG_DELTA_X_L                   =0x03,        /**< delta_x_l  register */
    PMW3901_REG_DELTA_X_H                   =0x04,        /**< delta_x_h register */
    PMW3901_REG_DELTA_Y_L                   =0x05,        /**< delta_y_l register */
    PMW3901_REG_DELTA_Y_H                   =0x06,        /**< delta_y_h register */
    PMW3901_REG_SQUAL                       =0x07,        /**< squal register */
    PMW3901_REG_RAW_DATA_SUM                =0x08,        /**< raw data sum register */
    PMW3901_REG_MAXIMUM_RAW_DATA            =0x09,        /**< maximum raw data register */
    PMW3901_REG_MINIMUM_RAW_DATA            =0x0A,        /**< minimum raw data register */
    PMW3901_REG_SHUTTER_LOWER               =0x0B,        /**< shutter lower register */
    PMW3901_REG_SHUTTER_UPPER               =0x0C,        /**< shutter upper register */
    PMW3901_REG_OBSERVATION                 =0x15,        /**< observation register */
    PMW3901_REG_MOTION_BURST                =0x16,        /**< motion burst register */
    PMW3901_REG_POWER_UP_RESET              =0x3A,        /**< power up reset register */
    PMW3901_REG_SHUTDOWN                    =0x3B,        /**< shutdown register */
    PMW3901_REG_RAW_DATA_GRAB               =0x58,        /**< raw data grab register */
    PMW3901_REG_RAW_DATA_GRAB_STATUS        =0x59,        /**< raw data grab status register */
    PMW3901_REG_INVERSE_PRODUCT_ID          =0x5F,        /**< inverse product id register */

}pmw3901_reg_en;


typedef struct
{
    int16_t delta_x;
    int16_t delta_y;
    pmw3901_status flag;
}pmw3901_motion_data_st,*Ppmw3901_motion_data_st;


class PMW3901
{
private:

    CHIP_SPI* spi; //SPI最大2MHZ
    CHIP_OUTPUT_PIN *reset_pin;
    CHIP_INPUT_PIN* motion_pin;
    rt_mutex_t shift_lock;
    double shift_combine;

    pmw3901_status init_flag;
    uint8_t debug_flag;
public:
    PMW3901(CHIP_SPI* spi,
    CHIP_OUTPUT_PIN *reset_pin,
    CHIP_INPUT_PIN* motion_pin);
    ~PMW3901();
    pmw3901_status PMW3901_init();
    pmw3901_status PMW3901_burst_read_motion(Ppmw3901_motion_data_st p_motion);
    pmw3901_status PWM3901_count_shift_with_height(double height);//height mm
    double PWM3901_read_out_shift();
    pmw3901_status PWM3901_read_out_shift_and_reduce();
};


#endif /* _FLOW_SENSOR_HPP_ */
